package com.bw.ae.nexus.domain;

/**
 * 执行功能的json字符串
 * Created by Xiaosiyuan on 2016/8/23.
 */
public class JSonString {

    /**
     * 添加repository，所有?用projectLabel代替
     */
    public static String createRepo="{" +
            "  \"name\": \"repository\"," +
            "  \"type\": \"groovy\"," +
            "  \"content\": \"repository.createMavenHosted('ae-?-snapshot', org.sonatype.nexus.blobstore.api.BlobStoreManager.DEFAULT_BLOBSTORE_NAME, true, org.sonatype.nexus.repository.maven.VersionPolicy.SNAPSHOT, org.sonatype.nexus.repository.storage.WritePolicy.ALLOW, org.sonatype.nexus.repository.maven.LayoutPolicy.STRICT);repository.createMavenHosted('ae-?-release', org.sonatype.nexus.blobstore.api.BlobStoreManager.DEFAULT_BLOBSTORE_NAME, true, org.sonatype.nexus.repository.maven.VersionPolicy.RELEASE, org.sonatype.nexus.repository.storage.WritePolicy.ALLOW_ONCE, org.sonatype.nexus.repository.maven.LayoutPolicy.STRICT)\"" +
            "}";


    /**
     * 添加roles，所有?用projectLabel代替
     */
    public static String addRoles="{" +
            "  \"name\": \"role\"," +
            "  \"type\": \"groovy\"," +
            "  \"content\": \"security.addRole('ae-?-view','ae-?-view','ae-?-view',['nx-repository-view-maven2-ae-?-snapshot-*','nx-repository-view-maven2-ae-?-release-*'],[]);security.addRole('ae-?-admin','ae-?-admin','ae-?-admin',['nx-repository-view-maven2-ae-?-snapshot-*','nx-repository-view-maven2-ae-?-release-*','nx-repository-admin-maven2-ae-?-snapshot-*','nx-repository-admin-maven2-ae-?-release-*'],[])\"" +
            "}";

    /**
     * 添加user
     */
    public static String addUser="{" +
            "    \"name\": \"user\"," +
            "    \"type\": \"groovy\"," +
            "    \"content\": \"def manager = container.lookup(org.sonatype.nexus.security.SecuritySystem.class.name);" +
            "        try" +
            "        {" +
            "            def user=manager.getUser('userId',org.sonatype.nexus.security.user.UserManager.DEFAULT_SOURCE);" +
            "        }catch(org.sonatype.nexus.security.user.UserNotFoundException e)" +
            "        {" +
            "        security.addUser('userId','firstName','lastName','email',true,'password',['nx-anonymous','ae-public-view'])"+
            "        }\"" +
            "}";

    /**
     * 更新ae-public组信息
     */
    public static String updateRepo="{" +
            "  \"name\": \"updateRepository\"," +
            "  \"type\": \"groovy\"," +
            "  \"content\": \"def manager = container.lookup(org.sonatype.nexus.repository.manager.RepositoryManager.class.name);" +
            "  def repo=manager.get('ae-public');" +
            "  repo.stop();"+
            "  def exit=repo.getConfiguration().attributes('group').backing.get('memberNames');" +
            "  repo.update(new org.sonatype.nexus.repository.config.Configuration(repositoryName:'ae-public',recipeName: 'maven2-group',online:true,attributes:[storage: [ blobStoreName: 'default'],group:[memberNames:exit+'ae-?-snapshot'+'ae-?-release'],maven:[versionPolicy:'MIXED',layoutPolicy:'STRICT']]));" +
            "  repo.start();\"" +
            "}";

    /**
     * 向user中添加role信息
     */
    public static String updateUser="{" +
            "  \"name\": \"updateUser\"," +
            "  \"type\": \"groovy\"," +
            "  \"content\": \"def manager = container.lookup(org.sonatype.nexus.security.user.RoleMappingUserManager.class.name);" +
            "  def repo=manager.getUsersRoles('userId',org.sonatype.nexus.security.user.UserManager.DEFAULT_SOURCE);" +
            "  repo.add(new org.sonatype.nexus.security.role.RoleIdentifier(org.sonatype.nexus.security.user.UserManager.DEFAULT_SOURCE,'ae-?-view'));" +
            "  manager.setUsersRoles('userId',org.sonatype.nexus.security.user.UserManager.DEFAULT_SOURCE,repo)\"" +
            "}";

    /**
     *  更新user权限，若没有该用户，则添加
     */
    public static String updateOrAddUser="{" +
            "      \"name\": \"updateOrAddUser\"," +
            "      \"type\": \"groovy\"," +
            "      \"content\": \"def userManager = container.lookup(org.sonatype.nexus.security.user.UserManager.class.name); " +
            "      try" +
            "      {" +
            "            def user=userManager.getUser('userId');" +
            "            def mapManager = container.lookup(org.sonatype.nexus.security.user.RoleMappingUserManager.class.name);" +
            "            def repo=mapManager.getUsersRoles('userId',org.sonatype.nexus.security.user.UserManager.DEFAULT_SOURCE);" +
            "            repo.add(new org.sonatype.nexus.security.role.RoleIdentifier(org.sonatype.nexus.security.user.UserManager.DEFAULT_SOURCE,'ae-?-view'));" +
            "            mapManager.setUsersRoles('userId',org.sonatype.nexus.security.user.UserManager.DEFAULT_SOURCE,repo);" +
            "      }catch(org.sonatype.nexus.security.user.UserNotFoundException e)" +
            "      {" +
            "            security.addUser('userId','firstName','lastName','email',true,'password',['nx-anonymous','ae-public-view','ae-?-view'])" +
            "      }\"" +
            "}";

    /**
     * 取消用户对某个库的权限
     */
    public static String unGrantUser="{" +
            "    \"name\": \"unGrantUser\"," +
            "    \"type\": \"groovy\"," +
            "    \"content\": \"def manager = container.lookup(org.sonatype.nexus.security.user.RoleMappingUserManager.class.name);" +
            "        def repo=manager.getUsersRoles('userId',org.sonatype.nexus.security.user.UserManager.DEFAULT_SOURCE);" +
            "        repo.remove(new org.sonatype.nexus.security.role.RoleIdentifier(org.sonatype.nexus.security.user.UserManager.DEFAULT_SOURCE,'ae-?-view'));" +
            "        manager.setUsersRoles('userId',org.sonatype.nexus.security.user.UserManager.DEFAULT_SOURCE,repo);\"" +
            "}";

    /**
     * 修改用户密码，需要原密码
     */
    public static String changePassword="{" +
            "    \"name\": \"changePassword\"," +
            "    \"type\": \"groovy\"," +
            "    \"content\": \"def manager = container.lookup(org.sonatype.nexus.security.SecuritySystem.class.name);" +
            "        manager.changePassword('userId','oldPassword','newPassword');\"" +
            "}";

    /**
     * 修改用户密码，不需要原密码
     */
    public static String changePasswordForce="{" +
            "    \"name\": \"changePasswordForce\"," +
            "    \"type\": \"groovy\"," +
            "    \"content\": \"def manager = container.lookup(org.sonatype.nexus.security.SecuritySystem.class.name);" +
            "        manager.changePassword('userId','password');\"" +
            "}";
    /**
     * 删除用户
     */
    public static String deleteUser="{" +
            "    \"name\": \"deleteUser\"," +
            "    \"type\": \"groovy\",\n" +
            "    \"content\": \"def manager = container.lookup(org.sonatype.nexus.security.user.UserManager.class.name);" +
            "        manager.deleteUser('userId');\"" +
            "}";

        /**
         * 删除role
         */
    public static String deleteRole="{" +
            "    \"name\": \"deleteRole\"," +
            "    \"type\": \"groovy\"," +
            "    \"content\": \"def manager = container.lookup(org.sonatype.nexus.security.SecuritySystem.class.name);" +
            "        def auManager=manager.getAuthorizationManager(org.sonatype.nexus.security.user.UserManager.DEFAULT_SOURCE);" +
            "        auManager.deleteRole('ae-?-view');" +
                "    auManager.deleteRole('ae-?-admin')\"" +
            "}";

        /**
         * 删除repository
         */
        public static String deleteRepo="{" +
                "    \"name\": \"deleteRepo\"," +
                "    \"type\": \"groovy\"," +
                "    \"content\": \"def manager = container.lookup(org.sonatype.nexus.repository.manager.RepositoryManager.class.name);" +
                "        manager.delete('ae-?-release');" +
                "        manager.delete('ae-?-snapshot');\"" +
                "}";
        /**
         * 用户禁用
         */
        public static String disableUser="{"+
                "   \"name\": \"disableUser\","+
                "   \"type\": \"groovy\","+
                "   \"content\": \"def manager = container.lookup(org.sonatype.nexus.security.SecuritySystem.class.name);"+
                "   try{ "+
                "           def user=manager.getUser('userId',org.sonatype.nexus.security.user.UserManager.DEFAULT_SOURCE);"+
                "           user.setStatus(org.sonatype.nexus.security.user.UserStatus.disabled);"+
                "           manager.updateUser(user);"+
                "   }catch(org.sonatype.nexus.security.user.UserNotFoundException e){}\""+
                "}";
        /**
         * 重新激活用户
         */
        public static String activeUser="{"+
                "   \"name\": \"activeUser\","+
                "   \"type\": \"groovy\","+
                "   \"content\": \"def manager = container.lookup(org.sonatype.nexus.security.SecuritySystem.class.name);"+
                "   try{ "+
                "           def user=manager.getUser('userId',org.sonatype.nexus.security.user.UserManager.DEFAULT_SOURCE);"+
                "           user.setStatus(org.sonatype.nexus.security.user.UserStatus.active);"+
                "           manager.updateUser(user);"+
                "   }catch(org.sonatype.nexus.security.user.UserNotFoundException e){}\""+
                "}";
}
